<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

function sales_log_list_page()
{
    @$output = drupal_render(drupal_get_form('sales_log_list_form'));
    return $output;
}


function sales_log_list_form($form, &$form_state)
{
    global $user;
    $init_year = !empty($form_state['values']['year']) ? $form_state['values']['year'] : "";
    $init_month = !empty($form_state['values']['month']) ? $form_state['values']['month'] : "";
    _insert_date_selection_form($form, $form_state, 5, $init_year, $init_month);
    if(get_user_role_name($user->uid) === 'administrator'){

    }else{
        $form['employee_list'] =  _insert_loginuser_choose_salesman_selection($user);
    }
    $form['employee_salesman'] = get_all_salesman();
    $table['#markup'] = data_list_view_page($form, $form_state);
    ajax_submit_form($form, $form_state, $table, '');
    return $form;
}


function sales_log_list_form_validate($form, &$form_state)
{
}

function data_list_view_page($form = '', &$form_state = '')
{
        $output = '';
        //表头
        global $user;
        $user_info = _get_user_info();
        if(($user_info['manager'] == 0) && ($user_info['department'] == 1)){
            $header = array(
                array('data' => '选择', 'class' => ['essential no']),
                array('data' => '销售员', 'class' => ['essential salesman']),
                array('data' => '日期', 'class' => ['essential year']),
                array('data' => '客户', 'class' => ['essential client']),
                array('data' => '电话', 'class' => ['essential phone']),
                array('data' => '工作内容主题', 'class' => ['essential work_title']),
                array('data' => '登录地址', 'class' => ['essential address']),
                array('data' => '录入情况', 'class' => ['essential input']),
                array('data' => '操作', 'class' => ['essential more']),
            );
        }else{
            $header = array(
                array('data' => '选择', 'class' => ['essential no']),
                array('data' => '销售员', 'class' => ['essential salesman']),
                array('data' => '日期', 'class' => ['essential year']),
                array('data' => '客户', 'class' => ['essential client']),
                array('data' => '电话', 'class' => ['essential phone']),
                array('data' => '工作内容主题', 'class' => ['essential work_title']),
                array('data' => '录入情况', 'class' => ['essential input']),
                array('data' => '操作', 'class' => ['essential more']),
            );
        }

        $query = db_select('employee', 'E')->extend('PagerDefault')->extend('TableSort');
        $query->join('sales_log', 'L', 'L.salesman_id=E.employee_id');
        $query->fields('L', array('sales_log_id', 'sales_log_date', 'client', 'phone','work_title','log_ip', 'log_addr', 'author_uid'));
        $query->addField('E', 'employee_name');
        if (empty($form_state['values']) || empty($form_state['values']['order'])) {
            $query->orderBy('L.sales_log_date', 'DESC');
        }

        if (!empty($form_state['values']['salesman'])) {//查询条件
            if ($form_state['values']['salesman']) {
                $query->condition('e.employee_id', $form_state['values']['salesman'], '=');
            }
        }
        $datestr = '';
        if (!empty($form_state['values']['year'])) {//查询条件
            if ($form_state['values']['year']) {
                $datestr .= $form_state['values']['year'] . '-';
            }
        }
        if (!empty($form_state['values']['month'])) {//查询条件
            if ($month = $form_state['values']['month']) {
                if ($month < 10)
                    $datestr .= '0' . $form_state['values']['month'];
                else
                    $datestr .= $form_state['values']['month'];
            }
        }
        if (!empty($form_state['values']['employee_salesman'])) {//查询条件
            $query->condition('L.author_uid', $form_state['values']['employee_salesman'] , '=');
        }
        if(!user_access('view all sales_log') && user_access('view own sales_log')){
            $query->condition('L.author_uid',$user->uid, '=');
        }
        if ($datestr) {
            $query->condition('L.sales_log_date', db_like($datestr) . '%', 'LIKE');
        }
        if (!empty($form_state['values']['employee_list']) && $form_state['values']['employee_list'] != "default") {//查询条件
            if ($form_state['values']['employee_list']) {
                $query->condition('L.author_uid', $form_state['values']['employee_list']);
            }
        }
        if ($form_state['values']['employee_list'] == "default") {
            if($user_info['role'] != 'administrator'&& $user_info['role'] != '经销商') {
                $salesmans_ids = get_salesman_by_self($user);
                $query->condition('L.author_uid', $salesmans_ids, 'in');
            }
        }
        $query->limit(variable_get("list_page_item_number", 20));
        $sales_logInfo = $query->orderByHeader($header)->execute();

        $rows = array();
        //表内容
        $n = 1;
        foreach ($sales_logInfo as $row) {

            if (empty($row->log_ip)) {
                $checked = '<input type="checkbox" disabled="true" class="listViewTableInputCheck"/>';
                $domore = '<a href="'.base_path().'sales/sales_log/edit/' . $row->sales_log_id . '" data-toggle="modal" target = "_top"><img width="20px" height="20px" src="'.base_path().'sites/all/themes/tweme/images/images/cz.png"/></a>';
            } else {
                $checked = '<input type="checkbox" disabled="true" class="listViewTableInputCheck"' . 'checked="true"' . '"/>';
                $domore = '<a href="'.base_path().'sales/sales_log/edit/' . $row->sales_log_id . '" data-toggle="modal" target = "_top"><img width="20px" height="20px" src="'.base_path().'sites/all/themes/tweme/images/images/cz.png"/></a>';
            }

            if (nonghua_user_access('sales_log', 'delete', $row->sales_log_id, $row->author_uid, 0)) {
                $disabled = ' class="listViewTableCheck" ';
            } else{
                $disabled = ' disabled="true" ';
            }

            if(($user_info['manager'] == 0) && ($user_info['department'] == 1)) {

                $rows[] = array(
                    array('data' => '<input type="checkbox"'.$disabled.' value ="' . $row->sales_log_id . '"/>' . $n++, 'class' => ['essential no']),
                    array('data' => $row->employee_name, 'class' => ['essential name']),
                    array('data' => $row->sales_log_date, 'class' => ['essential year']),
                    array('data' => $row->client, 'class' => ['essential client']),
                    array('data' => $row->phone, 'class' => ['essential phone']),
                    array('data' => $row->work_title, 'class' => ['essential work_title']),
                    array('data' => $row->log_addr, 'class' => ['essential phone']),
                    array('data' => $checked, 'class' => ['essential no']),
                    array('data' => $domore, 'class' => ['essential more']),
                );
            }else{
                $rows[] = array(
                    array('data' => '<input type="checkbox"'.$disabled.' value ="' . $row->sales_log_id . '"/>' . $n++, 'class' => ['essential no']),
                    array('data' => $row->employee_name, 'class' => ['essential name']),
                    array('data' => $row->sales_log_date, 'class' => ['essential year']),
                    array('data' => $row->client, 'class' => ['essential client']),
                    array('data' => $row->phone, 'class' => ['essential phone']),
                    array('data' => $row->work_title, 'class' => ['essential work_title']),
                    array('data' => $checked, 'class' => ['essential no']),
                    array('data' => $domore, 'class' => ['essential more']),
                );
            }
        }

        $output .= theme('table', array('header' => $header, 'rows' => $rows, 'empty' => t('没有记录'), 'attributes' => array('class' => array('table table-bordered table-striped table-hover enhanced')))); //表格
        /*	 * *按钮** */
        if(sizeof($rows) > 0 && (user_access('delete own sales_log') ||  user_access('delete all sales_log'))) {
            $output .= '<a href="javascript:void(0)" id="listViewTableSelectAll" data-toggle="modal" class="btn btn-mini">全选</a>';
            $output .= '<a href="javascript:void(0)" id="listViewTableDeleteSelected" data-toggle="modal" class="btn btn-mini">删除所选</a>';
        }
        $output .= '<a href="'.base_path().'sales/sales_log/export?year='.$form_state['values']['year'].'&month='.$form_state['values']['month'].'&employee_salesman='.$form_state['values']['employee_salesman'].'" data-toggle="modal" class="btn btn-mini">导出</a>';
        $output .= theme('pager');
        return $output;
}

function sales_log_delete()
{
    ajax_table_delete('sales_log', 'sales_log_id');
}

function getday($date)
{
    return getdate(strtotime($date));
}

function sales_log_export($form,&$form_state){
    $letter = array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
    $tableheader = array('地址','销售员','日期','客户','电话','工作内容主题','详细地址','联系人','联系电话','详情或心得');
    $data = get_sales_log_data($form, $form_state);
    nonghua_php_excel($letter,$tableheader,$data,'工作日志数据');
}

function get_sales_log_data($form = '', &$form_state = ''){
    global $user;
    $user_info = _get_user_info($user->uid);
    $query = db_select('employee', 'E');
    $query->join('sales_log', 'L', 'L.salesman_id=E.employee_id');
    $query->join('addr','A','L.addr_id = A.addr_id');
    $query->leftJoin('addr', 'A1', 'A.province_id=A1.addr_id');
    $query->leftJoin('addr', 'A2', 'A.city_id=A2.addr_id');
    $query->leftJoin('addr', 'A3', 'A.county_id=A3.addr_id');
    $query->leftJoin('addr', 'A4', 'A.town_id=A4.addr_id');
    $query->leftJoin('addr', 'A5', 'A.village_id=A5.addr_id');


    if (!empty($_GET['salesman'])) {//查询条件
        if ($_GET['salesman']) {
            $query->condition('e.employee_id', $_GET['salesman'], '=');
        }
    }
    $datestr = '';
    if (!empty($_GET['year'])) {//查询条件
        if ($_GET['year']) {
            $datestr .= $_GET['year'] . '-';
        }
    }
    if (!empty($_GET['month'])) {//查询条件
        if ($month = $_GET['month']) {
            if ($month < 10)
                $datestr .= '0' . $_GET['month'];
            else
                $datestr .= $_GET['month'];
        }
    }
    if (!empty($_GET['employee_salesman'])) {//查询条件
        $query->condition('L.author_uid', $_GET['employee_salesman'] , '=');
    }
    if(!user_access('view all sales_log') && user_access('view own sales_log')){
        $query->condition('L.author_uid',$user->uid, '=');
    }
    if ($datestr) {
        $query->condition('L.sales_log_date', db_like($datestr) . '%', 'LIKE');
    }
    if (!empty($_GET['employee_list']) && $_GET['employee_list'] != "default") {//查询条件
        if ($_GET['employee_list']) {
            $query->condition('L.author_uid', $_GET['employee_list']);
        }
    }
    if ($_GET['employee_list'] == "default") {
        if($user_info['role'] != 'administrator'&& $user_info['role'] != '经销商') {
            $salesmans_ids = get_salesman_by_self($user);
            $query->condition('L.author_uid', $salesmans_ids, 'in');
        }
    }
    $query->addField('A1', 'name', 'province');
    $query->addField('A2', 'name', 'city');
    $query->addField('A3', 'name', 'county');
    $query->addField('A4', 'name', 'town');
    $query->addField('A5', 'name', 'village');
    $query->addField('E', 'employee_name');
    $query->fields('L', array('sales_log_date', 'client', 'phone','work_title','addr_details', 'contact', 'phone','work_details'));
    $sales_logInfo = $query->execute()->fetchAll();
    foreach($sales_logInfo as $k => $v){
        $sales_logInfo[$k]->province= $v->province.$v->city.$v->county.$v->town.$v->village;
        unset($sales_logInfo[$k]->city);
        unset($sales_logInfo[$k]->county);
        unset($sales_logInfo[$k]->town);
        unset($sales_logInfo[$k]->village);
    }
    return $sales_logInfo;
}